package com.company.huisuo.biz.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.company.huisuo.base.BaseSingleAdapter
import com.company.huisuo.biz.bean.GiftBean
import com.company.huisuo.databinding.ItemGiftBinding
import com.company.huisuo.ext.loadImg


class ArtistGiftAdapter : BaseSingleAdapter<GiftBean, ItemGiftBinding>() {

    private var mSelectPos = 0

    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemGiftBinding
        get() = ItemGiftBinding::inflate

    override fun convert(holder: SingleViewHolder<ItemGiftBinding>, position: Int, item: GiftBean?) {
        item?.let { gift ->
            holder.binding.ivIcon.loadImg(gift.giftLogo)
            holder.binding.tvName.text = "${gift.giftName}\n${gift.giftAmount}"
            holder.binding.clLayout.isSelected = mSelectPos == position
            holder.binding.tvSend.isVisible = mSelectPos == position
            holder.binding.clLayout.setOnClickListener {
                mSelectPos = position
                notifyDataSetChanged()
            }
        }
    }
    
    /**
     * 获取当前选中的礼物
     */
    fun getSelectedGift(): GiftBean? {
        return if (mSelectPos >= 0 && mSelectPos < itemCount) {
            getItem(mSelectPos)
        } else {
            null
        }
    }
}